/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2024-07-16 10:29:01
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2024-07-16 10:46:33
 * @FilePath: /vue3-uniapp/src/service/app/useKeyboard.ts
 */

export const KEYBOARD = 'keyboard';

export const useKeyboard = () => {
    const keyboardHeightChangeList = ref<((h: number) => void)[]>([]);
    const onKeyboardHeightChange = (fn: (keyboardHeight: number) => void) => {
        keyboardHeightChangeList.value.push(fn);
    };

    const handleKeyboardHeightChange = (keyboardHeight: number) => {
        keyboardHeightChangeList.value.forEach((fn) => {
            fn(keyboardHeight);
        });
    };

    uni.$on(KEYBOARD, handleKeyboardHeightChange);

    onBeforeUnmount(() => {
        uni.$off(KEYBOARD, handleKeyboardHeightChange);
    });

    return {
        onKeyboardHeightChange
    };
};
